<?php

class Admin_CityController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
        $adminValidation = new Validations_User();
        $valid = $adminValidation->hasidentity();
        if ($valid) {
            if ($adminValidation->getUserTypeId() != 1) {
                $this->view->message = "You are not admin.";
                Zend_Auth::getInstance()->clearIdentity();
                $this->_redirect('/admin/login');
            }
        } else {
            $this->_redirect('/admin/login');
        }
        $this->_helper->layout->setLayout('admin');
    }

    public function indexAction() {
        $db = new Ads_Model_City();
        $form = new Admin_Form_AddCity();
        $this->view->form = $form;
        $formErrors = array();
        if ($this->getrequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $values = $form->getValues();
                if ($values['state'] != -1) {
                    $cname = $values['name'];
                    $data = array(
                        'name' => htmlspecialchars($cname),
                        'state_id' => $values['state']
                    );
                    $db->insert($data);
                } else {
                    array_push($formErrors, "You must select state.");
                }
                if (sizeof($formErrors) > 0) {
                    $this->view->formErrors = $formErrors;
                }
            }
        }
        $all = $db->fetchAll();
        $stateDb = new Ads_Model_State();
        $stateData = $stateDb->fetchAll();
        $this->view->allCity = $all;
        $this->view->allState = $stateData;        
    }

    public function updateAction() {
        $cityId = $this->getinputidAction();
        $cityDb = new Ads_Model_City();
        $cityData = $cityDb->getCityById($cityId);
        $form = new Admin_Form_UpdateCity();
        $form->main($cityData[0]);
        $this->view->form = $form;
        $formErrors = array();
        if ($this->getrequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $values = $form->getValues();
                if ($values['state'] != -1) {
                    $data = array(
                        'name' => htmlspecialchars($values['name']),
                        'state_id' => $values['state']
                    );
                    $cityDb->updateCity($data, $values['id']);
                    $this->_redirect('/city');
                } else {
                    array_push($formErrors, "You must select state.");
                }
                if (sizeof($formErrors) > 0) {
                    $this->view->formErrors = $formErrors;
                }
            }
        }
    }

    /**
     * Get City ID.    
     */
    public function getinputidAction() {
        $filters = array(
            'id' => array('HtmlEntities', 'StripTags', 'StringTrim')
        );
        $validators = array(
            'id' => array('Int')
        );
        $input = new Zend_Filter_Input($filters, $validators);
        $input->setData($this->getRequest()->getParams());
        $id = $input->id; //city id
        return $id;
    }

}

